package com.itheima.controller;

import com.itheima.domain.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    //删除角色
    @RequestMapping("/del/{roleId}")
    public String del(@PathVariable("roleId") Long roleId){
        roleService.del(roleId);
        return "redirect:/role/list";
    }

    //新建角色
    @RequestMapping("/save")
    public String save(Role role){
        boolean flag = roleService.save(role);
        return "redirect:/role/list";
    }


    //角色列表展示
    @RequestMapping("/list")
    public ModelAndView list(ModelAndView modelAndView){

//        调用roleService中的list()
        List<Role> roleList = roleService.list();
//        设置对象模型
        modelAndView.addObject("roleList",roleList);
//        设置页面视图
        modelAndView.setViewName("role-list");
//        返回页面视图
        return modelAndView;
    }
}


//package com.itheima.controller;
//
//import com.itheima.domain.Role;
//import com.itheima.service.RoleService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.servlet.ModelAndView;
//
//import java.util.List;
//
//@RequestMapping("/role")
//@Controller
//public class RoleController {
//
//    @Autowired
//    private RoleService roleService;
//
//    @RequestMapping("/list")
//    public ModelAndView list(){
//        ModelAndView modelAndView = new ModelAndView();
//        List<Role> roleList = roleService.list();
//        //设置模型
//        modelAndView.addObject("roleList",roleList);
//        //设置视图
//        modelAndView.setViewName("role-list");
//        System.out.println(roleList);
//        return modelAndView;
//    }
//
//}

